package cn.com.sparkle.paxos.net.netlayer.netty;


import cn.com.sparkle.paxos.net.frame.FrameBody;
import cn.com.sparkle.paxos.net.frame.FrameHead;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class CheckSumPackageEncoder extends MessageToByteEncoder<FrameBody> {
	@Override
	protected void encode(ChannelHandlerContext ctx, FrameBody msg, ByteBuf out) throws Exception {
		FrameHead head = new FrameHead(msg.getChecksumType(), msg.getBodySize());
		out.ensureWritable(head.getSerializeSize() + msg.getSerializeSize());
		out.writeBytes(head.getHead());
		out.writeBytes(head.getChecksum());
		out.writeBytes(msg.getBody());
		out.writeBytes(msg.getChecksum());
	}
}
